mod shape;
mod align;
mod layout;
mod table;
mod tabs;

use std::fmt::{Display, Formatter};
use xlui::*;
use crate::align::TestAlign;
use crate::layout::TestLayout;
use crate::shape::TestShape;
use crate::table::TestTable;
use crate::tabs::TestTabWidget;

#[derive(PartialEq)]
enum TestKind {
    Shape,
    // Widgets,
    Align,
    ChildWindow,
    Table,
    Layout,
    TabWidget,
}

impl Display for TestKind {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            TestKind::Shape => f.write_str("图形"),
            // TestKind::Widgets => f.write_str("控件"),
            TestKind::Align => f.write_str("对齐"),
            TestKind::ChildWindow => f.write_str("新窗口"),
            TestKind::Table => f.write_str("表格"),
            TestKind::TabWidget => f.write_str("选项卡"),
            TestKind::Layout => f.write_str("布局")
        }
    }
}

#[test]
fn main() {
    XlUi::new().run().unwrap_or_else(|e| println!("{}", e.to_string()));
}

struct XlUi {}

impl XlUi {
    pub fn new() -> XlUi {
        XlUi {}
    }

    fn on_rect_close(&mut self, window: InnerWindow, _: &mut Ui) {
        let _: TestShape = window.to_();
    }


    fn open_test_shape(&mut self, _: &mut Button, ui: &mut Ui) {
        ui.create_inner_window(TestShape::new()).on_close(Self::on_rect_close);
    }

    // fn open_test_widgets(&mut self, _: &mut Button, ui: &mut Ui) {
    //     ui.create_inner_window(TestWidget::new());
    // }

    fn open_test_align(&mut self, _: &mut Button, ui: &mut Ui) {
        ui.create_inner_window(TestAlign::new());
    }

    fn open_child_window(&mut self, _: &mut Button, ui: &mut Ui) {
        ui.create_window(TestShape::new());
    }

    fn open_test_table(&mut self, _: &mut Button, ui: &mut Ui) {
        ui.create_window(TestTable::new());
    }

    fn open_test_layout(&mut self, _: &mut Button, ui: &mut Ui) {
        ui.create_inner_window(TestLayout {});
    }

    fn open_tab_widget(&mut self, _: &mut Button, ui: &mut Ui) {
        ui.create_inner_window(TestTabWidget::new());
    }
}

impl App for XlUi {
    fn draw(&mut self, ui: &mut Ui) {
        ui.horizontal(|ui| {
            ui.button(TestKind::Shape).set_callback(Self::open_test_shape);
            // ui.button(TestKind::Widgets).set_callback(Self::open_test_widgets);
            ui.button(TestKind::Align).set_callback(Self::open_test_align);
            ui.button(TestKind::ChildWindow).set_callback(Self::open_child_window);
            ui.button(TestKind::Table).set_callback(Self::open_test_table);
            ui.button(TestKind::Layout).set_callback(Self::open_test_layout);
            ui.button(TestKind::TabWidget).set_callback(Self::open_tab_widget);
        });
    }
    fn window_attributes(&self) -> WindowAttribute {
        WindowAttribute {
            inner_size: (1920, 1080).into(),
            ..Default::default()
        }
    }
}